// pages/myback/myback.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageRoot: app.globalData.imageRoot,
    ani: null,
    num: 1,
    yy: [],
    qy: [],
    myl: false,
    page_token: 0,
    showrole: false,
    identity: ''
  },
  marketInfo(datalist) {
    let that = this;
    //获取用户信息
    let liky = datalist.results.filter((p) => {
      return p.client
    })
    let v = [];
    for (let i = 0; i < liky.length; i++) {
      v[i] = liky[i].client
    }
    getApp().ports.api38({
      data: v
    }).then((res) => {
      for (let i in datalist.results) {
        datalist.results[i].createTime = app.timeFormat(datalist.results[i].createTime, true);
        for (let j in res.data) {
          if (datalist.results[i].client == res.data[j].id) {
            datalist.results[i].markName = res.data[j].nickname
            datalist.results[i].avatar_url = res.data[j].avatar_url
            datalist.results[i].telphone = res.data[j].phone
            var obj = {
              name: res.data[j].nickname,
              phone: res.data[j].phone,
              img: res.data[j].avatar_url,
              client: datalist.results[i].client,
              no: datalist.results[i].no,
              // createTime:user.results[i].createTime
            }
            if (that.data.num === 1) {
              if (that.data.identity === '市场专员' || that.data.identity === '合伙专员') {
                console.log(' 市场专员')
                if (datalist.results[i].type == 1 || datalist.results[i].type == 2 || datalist.results[i].type == 4) {
                  datalist.results[i].url = '../yyDetails/yyDetails?typeId=' + datalist.results[i].follow_id + '&type=' + datalist.results[i].type + '&user=' + encodeURIComponent(JSON.stringify(obj))
                } else {
                  datalist.results[i].url = '../fYdetails/fYdetails?typeId=' + datalist.results[i].follow_id + '&type=' + datalist.results[i].type + '&user=' + encodeURIComponent(JSON.stringify(obj))
                }
              } else {
                if (datalist.results[i].type == 1 || datalist.results[i].type == 2 || datalist.results[i].type == 4) {
                  datalist.results[i].url = '../fpReserve/fpReserve?id=' + datalist.results[i].id + '&type=' + datalist.results[i].type
                } else {
                  datalist.results[i].url = '../fpyy/fpyy?id=' + datalist.results[i].id + '&type=' + datalist.results[i].type
                }
              }
            } else if (that.data.num === 2) {
              switch (datalist.results[i].type) {
                case 1:
                  datalist.results[i].url = '../../subpackages/pages/fpsign/fpsign?id=' + datalist.results[i].id
                  break;
                case 2:
                  datalist.results[i].url = '../../subpackages/pages/discount/discount?id=' + datalist.results[i].id
                  break;
                case 3:
                  if (datalist.results[i].stage >= 3) {
                    datalist.results[i].url = '../fplisting/fplisting?id=' + datalist.results[i].id
                  } else {
                    datalist.results[i].url = '../../subpackages/pages/listing/listing?id=' + datalist.results[i].id
                  }
                  break;
              }
            }
          }
        }
      }
      that.setData({
        yy: datalist,
        qy: datalist
      })
    }).catch(() => {
      console.error('批量获取用户电话号码！')
    })
  },
  onReachBottom() { //上拉加载更多
    var that = this;
    var active = that.data.num;
    var type = ['', that.data.yy, that.data.qy];
    //检查是否还有数据可以加载
    var total_size = type[active].total;
    var size = type[active].results.length;
    var page_token = that.data.page_token += 1
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        page_size: 10,
        page_token: page_token
      }
      that.api(data)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      that.setData({
        myl: true,
      })
    }
    that.setData({
      page_token: page_token
    })
  },
  itemClick: function (e) {
    var that = this;
    that.setData({
      num: Number(e.currentTarget.dataset.num),
      page_token: 0
    })
    var data = {
      page_size: 10
    }
    that.api(data)
  },
  navTo: function (e) {
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  api(data) {
    var that = this;
    var active = that.data.num;
    wx.showLoading({
      title: '加载中',
    })
    if (active == 1) {
      getApp().ports.apiYyallUser({
        data
      }).then((res) => {
        var res = res.data;
        
        res.results.forEach(item => {
          switch (item.type) {
            case 2:
              item.banner = that.data.imageRoot + item.banner;
              break;
            case 3:
              if (item.banner) {
                item.banner = that.data.imageRoot + item.banner
              }else{
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg';
              }
              break;
            case 5:
              item.banner = 'https://api.yipaiju.com/resources/images/wawa/hf.png'
              break;
            case 6:
              item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
              break;
          }
          var obj = {
            name: item.markName,
            phone: item.telphone,
            img: item.img,
            client: item.client,
            no: item.no,
            // createTime:user.results[i].createTime
          }
          // if (that.data.identity === '市场专员' || that.data.identity === '合伙专员') {
          //   // followId 应该是id
          //   if (item.type == 1 || item.type == 2 || item.type == 4) {
          //     item.url = '../yyDetails/yyDetails?typeId=' + item.followId + '&type=' + item.type + '&user=' + encodeURIComponent(JSON.stringify(obj))
          //   } else {
          //     item.url = '../fYdetails/fYdetails?typeId=' + item.followId + '&type=' + item.type + '&user=' + encodeURIComponent(JSON.stringify(obj))
          //   }
          // } else {
          //   if (item.type == 1 || item.type == 2 || item.type == 4) {
          //     item.url = '../fpReserve/fpReserve?id=' + item.id + '&type=' + item.type
          //   } else {
          //     item.url = '../fpyy/fpyy?id=' + item.id + '&type=' + item.type
          //   }
          // }

          item.createTime = app.timeFormat(item.createTime, true)
          if (that.data.page_token >= 1) {
            that.data.yy.results.push(item)
          }
        })
        if (that.data.page_token >= 1) {
          res.results = that.data.yy.results
        }
        
        // that.marketInfo(res);
        wx.hideLoading({
          success: (res) => {},
        })
        that.setData({
          yy: res
        })

      }).catch(() => {
        console.error('出错');
      });
    } else if (active == 2) {
      getApp().ports.api19({
        data: data
      }).then((res) => {
        var res = res.data;
        res.results.forEach(item => {
          item.createTime = app.timeFormat(item.createTime)
          if(item.type == 2) item.banner = that.data.imageRoot + item.banner;
          if(item.type == 3) {
            if (item.banner) {
              item.banner = that.data.imageRoot + item.banner
            }else{
              item.banner = 'https://api.yipaiju.com/resources/images/wawa/index_1.jpg';
            }
          }
          if (that.data.page_token >= 1) {
            that.data.qy.results.push(item)
          }
        })
        if (that.data.page_token >= 1) {
          res.results = that.data.qy.results
        }
       
        wx.hideLoading({
          success: (res) => {},
        })
        // that.marketInfo(res);
        that.setData({
          qy: res
        })

      }).catch(() => {
        console.error('出错');
      });
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var data = {
      page_size: 10,
      page_token: 0
    }
    that.setData({
      identity: wx.getStorageSync('identity')
    })
    that.api(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var animation = wx.createAnimation({
    //   duration: 4000,
    //   timingFunction: 'ease',
    //   delay: 0,
    //   transformOrigin:"50% 50%"
    //  });
    //  animation.translateX(200).opacity(1).step()
    //  this.setData({
    //   ani: animation.export()
    //  })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    this.setData({
      page_token: 0,
      identity:wx.getStorageSync('identity')
    })
    console.log('下拉刷新', that.data.identity)
    var data = {
      page_size: 10,
      page_token: 0
    }
    that.api(data)
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },
})